package de.midnightdance.admin.ui.logIn;


import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import de.midnightdance.admin.Activator;
import de.midnightdance.admin.util.WebServiceProvider;
import de.midnightdance.admin.ws.WebServiceAdminRequiem;
import de.midnightdance.admin.ws.entities.RequiemUser;


public class UserLoginDialog {
	private 		Display display;
	private			Shell shell;
	
	private Text txt_Password;
	private Text txt_Username;
	private Label loginMessage;
	
	private LoginListener 	loginListener;
	private AbortListener 	abortListener;
	private DragListener 	dragListener;
	
	private RequiemUser loggedUser;
	
	private static Color BACKGROUND	= Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
	private static Color SHELL_RIM 	= Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
	private static Color DENIED 	= Display.getCurrent().getSystemColor(SWT.COLOR_RED);
	private static Color ACCEPTED	= Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);
	

	/**
	 * @wbp.parser.entryPoint
	 */
	public UserLoginDialog(Display display) {
		this.display = display;
		//
		loggedUser 	= null;		
		//
		loginListener 	= new LoginListener();
		abortListener 	= new AbortListener();
		dragListener	= new DragListener();
	}
	
	public RequiemUser getUser() {		
		return loggedUser;
	}

	public void openShell(){
		shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);		
		final GridLayout layout = new GridLayout(1,false);		
		layout.marginHeight			= 1;
		layout.marginWidth			= 1;		
		shell.setLayout(layout);
		//
		shell.setBackground(SHELL_RIM);
		//
		createContentsComposites(shell);
		//
		shell.setLocation(320,290);
		shell.pack();
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}	
	}
	
	private void createContentsComposites(Composite parent) {
		//
		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns		 = 2;
		gridLayout.marginHeight 	 = 0;
		gridLayout.verticalSpacing 	 = 0;
		gridLayout.marginWidth 		 = 0;
		gridLayout.horizontalSpacing = 0;
		composite.setLayout(gridLayout);		
		//
		composite.addListener(SWT.MouseDown	, dragListener);
		composite.addListener(SWT.MouseUp	, dragListener);
		composite.addListener(SWT.MouseMove	, dragListener);
        
		createImageComposite(composite);   
		
		createLoginComposite(composite);		
	}	
	
	private void createLoginComposite(Composite composite) {
		final Composite cmp_Login = new Composite(composite, SWT.NONE);
		final GridData gridData = new GridData(GridData.FILL_BOTH);		
		cmp_Login.setLayoutData(gridData);
		//
		final GridLayout layout = new GridLayout(2,false);		
		cmp_Login.setLayout(layout);
		//
		cmp_Login.setBackground(BACKGROUND);	
		//
		createLoginLabel(cmp_Login);
		createUserNameComposite(cmp_Login);	
	    createPasswordComposite(cmp_Login);
	    //
	    createButtons(cmp_Login);
		
	}

	private void createImageComposite(Composite composite){
		Label img_Label = new Label(composite, SWT.NONE);
		
		img_Label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		ImageDescriptor imageDesc = Activator.getImageDescriptor("pics/img_login.bmp");
		Image img = imageDesc.createImage();
		img_Label.setImage(img);
		img_Label.setLayoutData(new GridData(GridData.FILL_BOTH));
		//
		img_Label.addListener(SWT.MouseDown	, dragListener);
		img_Label.addListener(SWT.MouseUp	, dragListener);
		img_Label.addListener(SWT.MouseMove	, dragListener);		
    }
	
	/**
	 * @wbp.parser.entryPoint
	 */
	private void createLoginLabel(Composite cmp_Login){
		final CLabel clbl_UserLogin = new CLabel(cmp_Login, SWT.NONE);
		final GridData rowData = new GridData(SWT.LEFT,SWT.CENTER,false,false,2,1);		
		clbl_UserLogin.setLayoutData(rowData);
		clbl_UserLogin.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		clbl_UserLogin.setText("Login to Reqiuem DB Admin");
	}
	
	private void createUserNameComposite(Composite cmp_Login){
		
		final CLabel clbl_Username = new CLabel(cmp_Login, SWT.NONE);
		final GridData gd_lbl_Username = new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1);		
		clbl_Username.setLayoutData(gd_lbl_Username);		
		clbl_Username.setBackground(BACKGROUND);
		clbl_Username.setText("Username");
		
		txt_Username = new Text(cmp_Login, SWT.BORDER|SWT.CENTER);
		final GridData gd_txt_Username = new GridData(SWT.FILL,SWT.CENTER,false,false,1,1);;
		txt_Username.setLayoutData(gd_txt_Username);
	}
	
	private void createPasswordComposite(Composite cmp_Login){
		
		final CLabel clbl_Password = new CLabel(cmp_Login, SWT.NONE);
		final GridData gd_lbl_Password = new GridData(SWT.LEFT,SWT.CENTER,false,false,1,1);		
		clbl_Password.setLayoutData(gd_lbl_Password);		
		clbl_Password.setBackground(BACKGROUND);
		clbl_Password.setText("Passwort");
		
		txt_Password = new Text(cmp_Login, SWT.BORDER|SWT.CENTER);
		final GridData gd_txt_Password = new GridData(SWT.FILL,SWT.CENTER,true,false,1,1);;
		txt_Password.setLayoutData(gd_txt_Password);
		txt_Password.setEchoChar('*');
	}
	
	private void createButtons(Composite cmp_Login){
		final Composite cmp_ButtonBar = new Composite(cmp_Login, SWT.NONE);
		GridData gd_cmp_ButtonBar = new GridData(SWT.FILL,SWT.FILL,true,true,2,1);
		cmp_ButtonBar.setLayoutData(gd_cmp_ButtonBar);
		GridLayout lo_cmp_ButtonBar = new GridLayout(2,false);
		cmp_ButtonBar.setLayout(lo_cmp_ButtonBar);
		cmp_ButtonBar.setBackground(BACKGROUND);
		//
		loginMessage = new Label(cmp_ButtonBar, SWT.NONE);
	    loginMessage.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false,2,1));	   
	    loginMessage.setBackground(BACKGROUND);		
		//
		final Button btn_abort = new Button(cmp_ButtonBar, SWT.FLAT);
		btn_abort.setLayoutData(new GridData(SWT.RIGHT,SWT.BOTTOM,true,true,1,1));
		btn_abort.setText("Abbruch");
		btn_abort.addSelectionListener(abortListener);
		//
		final Button btn_login = new Button(cmp_ButtonBar, SWT.FLAT);
		btn_login.setLayoutData(new GridData(SWT.LEFT,SWT.BOTTOM,false,true,1,1));
		btn_login.setText("Login");
		btn_login.addSelectionListener(loginListener);
		
	}
	
	
	
	
	private class DragListener implements Listener{
		private Point origin;
		@Override		
		public void handleEvent(Event e) {
			switch (e.type) {
			case SWT.MouseDown:
				origin = new Point(e.x, e.y);
				break;
			case SWT.MouseUp:
				origin = null;
				break;
			case SWT.MouseMove:
				if (origin != null) {
					Point p = display.map(shell, null, e.x, e.y);
					shell.setLocation(p.x - origin.x, p.y - origin.y);
				}
				break;
			}
		}		
	}
	
	private class LoginListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			RequiemUser user = new RequiemUser();
    		user.setName(txt_Username.getText());
    		user.setPass(txt_Password.getText());	
    		WebServiceAdminRequiem port = WebServiceProvider.getWebService();
    		RequiemUser authenticatedUser = port.isAuthenticUser(user);
    		if( authenticatedUser == null){
    			loginMessage.setText("Zugriff verweigert");    	
    			loginMessage.setForeground(DENIED);
    		}else{ 
    			loginMessage.setText("Zugriff gestattet");    	
    			loginMessage.setForeground(ACCEPTED);
    			loggedUser = authenticatedUser;
    			shell.close();
    		}	
		}		
	}
	
	private class AbortListener extends SelectionAdapter{
		@Override
		public void widgetSelected(SelectionEvent e) {
			shell.close();
		}		
	}
	
	
	public static void main(String[] args){
		Display display = new Display();
		UserLoginDialog dialog = new UserLoginDialog(display);
		dialog.openShell();
	}

	
}
